var text;
const db = wx.cloud.database()
const app = getApp();
const chooseLocation = requirePlugin('chooseLocation');
Page({
  data: {
    imgList: [],
    imgUrl: [],
    location: "",
    text:"",
    position:false
  },
 

  chooseLocation1() {
    const key = 'P6QBZ-RWGCX-FGK4S-7HQGU-ZHZOZ-3CBTM'; //使用在腾讯位置服务申请的key
    const referer = '青光志乡'; //调用插件的app的名称
    const location = JSON.stringify({
      latitude: 39.89631551,
      longitude: 116.323459711
    });
    const category = '志愿服务,娱乐休闲';
    wx.navigateTo({
      url: `plugin://chooseLocation/index?key=${key}&referer=${referer}&location=${location}&category=${category}`
    });
 setTimeout(()=>
 {
  this.setData({
    position:true
  })  
 }, 2000)
  },
  //选择图片的操作，每次选择图片后就把选择图片的数组黏在已有的图片数组后面
  afterRead(e) {
    this.data.imgList = this.data.imgList.concat(e.detail.file)
    console.log("动态发布", e)
    this.setData({
      imgList: this.data.imgList
    })
  },
  //删除图片
  //for循环找到要删除的哪张图片的url,然后使用数组的splice函数剔除它
  del(e) {
    console.log("删除", e)
    for (let i = 0; i < this.data.imgList.length; i++) {
      if (this.data.imgList[i].url == e.detail.file.url) {
        let list = []
        list = this.data.imgList
        list.splice(i, 1)
        this.setData({
          imgList: list
        })
      }
    }
  },
  getText(e) {
    console.log(e.detail.value)
    this.setData({
      text:e.detail.value
    })
    text = e.detail.value;
  },
  async pubText() {
    console.log("text",text)
    if(text="")
     {
       wx.showToast({
         title: '动态不能为空哦……',
       })
     }
     else{
      for (var a = 0; a < this.data.imgList.length; a++) {
         await wx.cloud.uploadFile({
          cloudPath: app.globalData.openid+text +Math.random()+ ".png",
          filePath: this.data.imgList[a].url, // 文件路径
        }).then(res => {
          console.log("文件", res.fileID)
          this.data.imgUrl.push({
            Url: res.fileID
          })
          console.log("this.data.imgUrl",this.data.imgUrl)
        })
      }
      let dd = this.data.imgUrl
      console.log("this.data.imgUrl1",this.data.imgUrl)
      console.log("dd",dd)
      db.collection("textAndPic")
        .add({
          data: {
            name: app.globalData.userInfo.userInfo.name,
            avaUrl: app.globalData.userInfo.userInfo.avaUrl,
            text: this.data.text,
            pubPerson: app.globalData.openid,
            imgUrl: dd,
            time: app.getTime(2),//具体的时间2022年2月30日
            time1: app.getTime(1),//时间戳
            location: this.data.location,
            comment: [],
            zan: []
          }
        })
        .then(res => {
          console.log("发布成功", res)
          wx.showToast({
            title: '动态发布成功^-^',
            icon: 'none',
            duration: 5000,
          })
          wx.switchTab({
            url: '../../project/project',
          })
        })
     }
  },
  onLoad: function (options) {
    console.log("tweun", app.globalData.userInfo)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var location1 = chooseLocation.getLocation();
    console.log("location1", location1)
    if (location1 != null)
      this.setData({
        location: location1
      })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})